<script setup>
import { ref } from 'vue'
const visiable = ref(false)
function closeByEsc(e) {
  if (e.key === 'Escape') {
    close()
  }
}
function open() {
  visiable.value = true
  window.addEventListener('keyup', closeByEsc)
}
function close() {
  visiable.value = false
  window.removeEventListener('keyup', closeByEsc)
}
defineExpose({ open, close })
</script>

<template>
  <Teleport to="body">
    <Transition name="popup">
      <div class="popup" v-if="visiable">
        <div class="popup-mask" @click="close"></div>
        <div class="popup-content">
          <slot></slot>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style lang="scss" scoped>
.popup-enter-from {
  opacity: 0.1;
}

.popup-enter-active,
.popup-leave-active {
  transition: opacity 0.5s;
}

.popup-enter,
.popup-leave-to {
  opacity: 0;
}

.popup,
.popup-mask {
  width: 100%;
  height: 100%;
}

.popup {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 200;

  .popup-mask {
    position: fixed;
    top: 0;
    left: 0;
    background-color: var(--mask-backdrop);
    z-index: 201;
  }

  .popup-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 202;
    box-shadow: 0 0 10px var(--box-shadow-color);
    border-radius: 30px;
  }
}
</style>
